package com.zczc.web.controller;

import com.github.pagehelper.PageInfo;
import com.yishuifengxiao.common.tool.response.Response;
import com.yishuifengxiao.common.tool.response.Response.Const;
import com.zczc.dto.MessageInfoDTO;
import com.zczc.dto.MessageInfoUpdateDTO;
import com.zczc.entity.MessageInfo;
import com.zczc.service.MessageInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:Mark
 * @Description:
 * @Date: Created on 14:32 2018-12-13
 * @Modify by:
 */
@RestController
@RequestMapping("/zczc/messageInfo")
@Api(tags = {"站内信 消息接口"})
@Validated
public class MessageInfoController {

  @Autowired
  private MessageInfoService messageInfoService;

  @PostMapping("/publishMessageInfo")
  @ApiOperation(value = "发布消息")
  public Response publishMessageInfo(@ApiParam(required = true,value = "添加的消息对象",name = "messageInfoDTO") @RequestBody(required = true) @Validated MessageInfoDTO messageInfoDTO,
      BindingResult bindingResult){
    if(bindingResult.hasErrors()){
     return Response.badParam(bindingResult.getAllErrors().get(0).getDefaultMessage());
    }
    messageInfoService.addMessageInfoSelective(messageInfoDTO);
    return Response.suc();
  }

  @ApiOperation(value = "根据主键选择性的修改消息")
  @PutMapping("/updateMessageInfo")
  public Response updateMessageInfo(@ApiParam(required = true,name = "messageInfoUpdateDTO",value = "封装要修改的对象")
      @RequestBody(required = true) @NotNull(message = "messageInfoUpdateDTO不能为null") MessageInfoUpdateDTO messageInfoUpdateDTO){
    messageInfoService.updateMessageInfoSelective(messageInfoUpdateDTO);
    return Response.suc();
  }

  @ApiOperation(value = "删除消息 逻辑删除")
  @DeleteMapping("/deletedMessageInfo")
  public Response deletedMessageInfo( @ApiParam(required = true,name = "ids",value = "要删除的id值")
      @RequestBody(required = true) @NotNull(message = "ids不能为null") @NotEmpty(message = "ids不能为空") List<String> ids){
    messageInfoService.deleteMessageInfoByIdLogic(ids);
    return Response.suc();
  }

  @ApiOperation(value = "通过条件分页查询所有的消息")
  @GetMapping("/selectMessageInfoPages")
  public Response<PageInfo<MessageInfo> > selectMessageInfoPages(
     @ApiParam(required = false,name = "title",value = "消息标题") @RequestParam(required = false,value = "title")String title,
     @ApiParam(required = false,name = "content",value = "消息内容") @RequestParam(required = false,value = "content") String content,
     @ApiParam(required = false,name = "pageNum",value = "页码，默认为1") @RequestParam(required = false,value = "pageNum") @Min(value = 1,message = "pageNum最小为1") Integer pageNum,
     @ApiParam(required = false,name = "pageSize",value = "页大小，默认为20") @RequestParam(required = false,value = "pageSize") @Min(value = 1,message = "页大小最小为1") Integer pageSize){
     MessageInfo messageInfo=new MessageInfo();
    messageInfo.setTitle(title==null?null:title);
    messageInfo.setContent(content==null?null:content);
    PageInfo<MessageInfo> data = messageInfoService
        .selectMessageInfoByPage(messageInfo, pageSize, pageNum);
    return new Response<PageInfo<MessageInfo> >(Const.CODE_OK,Const.MSG_OK,data);
  }



}
